package com.rain.query;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页结果返回对象
 *
 * @param <T> 数据类型
 */
public class PaginationResult<T> {

    private Long totalCount;
    private Integer pageSize;
    private Integer pageNo;
    private Integer pageTotal;
    private List<T> list = new ArrayList<>();

    /**
     * 全参构造函数
     */
    public PaginationResult(Long totalCount, Integer pageSize, Integer pageNo, Integer pageTotal, List<T> list) {
        if (pageNo == null || pageNo <= 0) {
            this.pageNo = 1;
        } else {
            this.pageNo = pageNo;
        }
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.pageTotal = pageTotal;
        this.list = list != null ? list : new ArrayList<>();
    }

    /**
     * 无 pageTotal 的构造函数（适用于未计算总页数场景）
     */
    public PaginationResult(Long totalCount, Integer pageSize, Integer pageNo, List<T> list) {
        if (pageNo == null || pageNo <= 0) {
            this.pageNo = 1;
        } else {
            this.pageNo = pageNo;
        }
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.list = list != null ? list : new ArrayList<>();
        // 计算总页数
        this.pageTotal = totalCount == null ? 0 : (int) Math.ceil((double) totalCount / pageSize);
    }

    /**
     * 默认构造函数
     */
    public PaginationResult() {
    }

    // ================== Getter & Setter ==================

    public Long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Long totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(Integer pageTotal) {
        this.pageTotal = pageTotal;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list != null ? list : new ArrayList<>();
    }

    // ================== toString ==================

    @Override
    public String toString() {
        return "PaginationResult{" +
                "totalCount=" + totalCount +
                ", pageSize=" + pageSize +
                ", pageNo=" + pageNo +
                ", pageTotal=" + pageTotal +
                ", list=" + list +
                '}';
    }
}
